from datetime import datetime
import os

from common.send_dingding_message import SendDingding
from utils.handle_yaml import read_vars1
from utils.log_control import INFO, ERROR
from utils.read_yaml import ReadYaml
from common.send_email import SendEmail
from common.send_data import send_email_data, send_dingding_data


def run_case():
    # 记录开始时间
    start_time = datetime.now()
    read_yaml = ReadYaml()
    data = read_yaml.read_yaml()
    pytest_allure_cmd = data["pytest_allure_cmd"]
    os.system(pytest_allure_cmd["pytest_cmd"])
    os.system(pytest_allure_cmd["allure_cmd"])

    # source = "./allure-report"
    # destin = "./allure_report_zip/report.zip"
    # zip_files(source, destin)
    pass_list = read_vars1('pass_list')
    fail_list = read_vars1('fail_list')
    skip_list = read_vars1('skip_list')
    # 记录结束时间
    end_time = datetime.now()
    # 计算耗时（秒）
    total_time = (end_time - start_time).total_seconds()
    try:
        email_data = data["send_email"]
        send_email = SendEmail(**email_data)
        send_email.send_email_by_att(
            send_email_data(pass_list, fail_list, skip_list, start_time.strftime('%Y-%m-%d %H:%M:%S'), total_time),
            './report/report.html')
        dingding_data = data["send_dingding"]
        send_dingding = SendDingding(**dingding_data)
        send_dingding.send_dingtalk_markdown(
            send_dingding_data(pass_list, fail_list, skip_list, start_time.strftime('%Y-%m-%d %H:%M:%S'), total_time),
            './report/report.html')
    except Exception as e:
        ERROR.logger.error("发送失败")
        raise e
    else:
        INFO.logger.info("发送成功")


if __name__ == '__main__':
    run_case()
